package com.ttxs.uaa.api.common;

import com.ttxs.common.core.properties.TokenProperties;
import com.ttxs.common.core.vo.R;
import com.ttxs.common.core.vo.Token;
import com.ttxs.common.core.utils.AuthUtils;
import com.ttxs.uaa.api.BaseApi;
import com.ttxs.common.core.constant.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(Api.Common.Token.PREFIX)
@RequiredArgsConstructor
@Slf4j
public class TokenApi extends BaseApi {
    @Autowired
    private TokenProperties tokenProperties;


    @PostMapping(Api.Common.Token.REFRESH)
    public R refreshToken() {
        try {
            String token = getToken();
            Token tokenObj = AuthUtils.createOrRefreshToken(AuthUtils.getLoginUser(token), token);
            Map<String, Object> data = new HashMap<>();
            data.put("token", token);
            data.put("tokenExpireTime", tokenObj.getExpTime());
            log.info("token续期成功");
            return R.ok(data);
        } catch (Exception e) {
            log.error("token验证异常，自动续期失败", e);
            return R.error("token验证异常，自动续期失败");
        }
    }
}
